package com.example.demo528.service;

import com.example.demo528.domain.Ebook;
import com.example.demo528.domain.EbookExample;
import com.example.demo528.mapper.EbookMapper;
import com.example.demo528.req.EbookReq;
import com.example.demo528.resp.EbookResp;
import com.example.demo528.utils.CopyUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EbookService {
    @Resource
    private EbookMapper ebookMapper;

    public List<EbookResp> list(EbookReq req) {
        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();
        criteria.andNameLike("%"+req.getName()+"%");
        List<Ebook> ebookList=ebookMapper.selectByExample(ebookExample);

//        List<EbookResp> respList=new ArrayList<>();
//        for (Ebook ebook:ebookList) {
////            EbookResp ebookResp=new EbookResp();
////            ebookResp.setName(ebook.getName());
////            BeanUtils.copyProperties(ebook,ebookResp);
//            EbookResp ebookResp=CopyUtil.copy(ebook,EbookResp.class);
//            respList.add(ebookResp);
//        }
        List<EbookResp> respList=CopyUtil.copyList(ebookList, EbookResp.class);
        return respList;
    }
}
